package cn.ibizlab.odoo.core.odoo_resource.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iresource_mixin;
import cn.ibizlab.odoo.core.odoo_resource.domain.Resource_mixin;
import cn.ibizlab.odoo.core.odoo_resource.filter.Resource_mixinSearchContext;
import cn.ibizlab.odoo.core.odoo_resource.service.IResource_mixinService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[资源装饰] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Resource_mixinServiceImpl implements IResource_mixinService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iresource_mixinClientService resource_mixinClientService;

    @Override
    public boolean update(Resource_mixin resource_mixin) {
        boolean bOk = false ;
        Iresource_mixin clientModel = this.convert2Model(resource_mixin,null);
        resource_mixinClientService.update(clientModel) ;
        this.convert2Domain(clientModel,resource_mixin) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Resource_mixin> resource_mixins, int batchSize) {

                        //resource_mixinClientService.createBatch(resource_mixins,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iresource_mixin clientModel = resource_mixinClientService.createModel();
        clientModel.setId(id);
        resource_mixinClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //resource_mixinClientService.removeBatch(resource_mixins) ;

    }

    @Override
    public void updateBatch(List<Resource_mixin> resource_mixins, int batchSize) {

        //resource_mixinClientService.updateBatch(resource_mixins,batchSize) ;

    }

    @Override
    public Resource_mixin get(Integer id) {
        Iresource_mixin clientModel = resource_mixinClientService.createModel();
        clientModel.setId(id);
        resource_mixinClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Resource_mixin get(Resource_mixin resource_mixin) {
        Resource_mixin retresource_mixin = this.get(resource_mixin.getId()) ;
    	BeanCopier copier=BeanCopier.create(retresource_mixin.getClass(), resource_mixin.getClass(), false);
        copier.copy(retresource_mixin, resource_mixin, null);
        return resource_mixin ;
    }

    @Override
    public boolean create(Resource_mixin resource_mixin) {
        boolean bOk = false ;
        Iresource_mixin clientModel = this.convert2Model(resource_mixin,null);
        resource_mixinClientService.create(clientModel) ;
        this.convert2Domain(clientModel,resource_mixin);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Resource_mixin> searchDefault(Resource_mixinSearchContext context) {
        List<Resource_mixin> list = new ArrayList<Resource_mixin>();
        Page<cn.ibizlab.odoo.core.client.model.Iresource_mixin> clientModelList = resource_mixinClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iresource_mixin clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Resource_mixin>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据资源工作时间查询
     */
    @Override
    public Page<Resource_mixin> selectByOdoo_resource_calendar(Integer id){
        Resource_mixinSearchContext context = new Resource_mixinSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Resource_mixin> list = new ArrayList<Resource_mixin>();
        Page<cn.ibizlab.odoo.core.client.model.Iresource_mixin> clientModelList = resource_mixinClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iresource_mixin clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Resource_mixin>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据资源查询
     */
    @Override
    public Page<Resource_mixin> selectByOdoo_resource(Integer id){
        Resource_mixinSearchContext context = new Resource_mixinSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Resource_mixin> list = new ArrayList<Resource_mixin>();
        Page<cn.ibizlab.odoo.core.client.model.Iresource_mixin> clientModelList = resource_mixinClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iresource_mixin clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Resource_mixin>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Resource_mixin> selectByOdoo_company(Integer id){
        Resource_mixinSearchContext context = new Resource_mixinSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Resource_mixin> list = new ArrayList<Resource_mixin>();
        Page<cn.ibizlab.odoo.core.client.model.Iresource_mixin> clientModelList = resource_mixinClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iresource_mixin clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Resource_mixin>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iresource_mixin convert2Model(Resource_mixin domain , Iresource_mixin model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = resource_mixinClientService.createModel() ;
        }

         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getTzDirtyFlag())
            model.setTz(domain.getTz());
         if(domain.getResource_id_textDirtyFlag())
            model.setResource_id_text(domain.getResource_id_text());
         if(domain.getResource_calendar_id_textDirtyFlag())
            model.setResource_calendar_id_text(domain.getResource_calendar_id_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getResource_calendar_idDirtyFlag())
            model.setResource_calendar_id(domain.getResource_calendar_id());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getResource_idDirtyFlag())
            model.setResource_id(domain.getResource_id());
        return model ;
    }

    public Resource_mixin convert2Domain( Iresource_mixin model ,Resource_mixin domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Resource_mixin();
        }

        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getTzDirtyFlag())
            domain.setTz(model.getTz());
        if(model.getResource_id_textDirtyFlag())
            domain.setResource_id_text(model.getResource_id_text());
        if(model.getResource_calendar_id_textDirtyFlag())
            domain.setResource_calendar_id_text(model.getResource_calendar_id_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getResource_calendar_idDirtyFlag())
            domain.setResource_calendar_id(model.getResource_calendar_id());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getResource_idDirtyFlag())
            domain.setResource_id(model.getResource_id());
        return domain ;
    }


}
